package UI.abstract.resources.loader
{
	import UI.abstract.resources.item.LoadObj;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	public class MptLoader extends BaseLoader
	{
		private var loader : URLLoader;
		public function MptLoader(loadObj:LoadObj)
		{
			super(loadObj);
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener( Event.COMPLETE , onComplete );
			loader.addEventListener( IOErrorEvent.IO_ERROR , onIOError );
		}
		override public function load () : void
		{
			loader.load( new URLRequest( loadObj.url ) );
		}
		override protected function onComplete ( event : Event ) : void
		{
			var ba:ByteArray = loader.data as ByteArray;
			ba.uncompress();
			_content = new Object();
			
			_content.cellWidth = ba.readShort();
			_content.cellHeight = ba.readShort();
			_content.xLen = ba.readShort();
			_content.yLen = ba.readShort();
			_content.mapWidth = ba.readShort();
			_content.mapHeight = ba.readShort();
			_content.connectedPointY = ba.readShort();
			//MapConfig.update();
			//MapConst.update(mapWidth, mapHeight);
			//MapConnect.getInstance().updateSize(mapWidth, mapHeight);
			var result:Array = new Array();
			var item:Array;
			var i:int = 0;
			while(i < 10){
				//保留字段
				ba.readInt();
				i ++;
			}
			for(var py:int = 0; py < _content.yLen; py++){
				item = [];
				result.push(item);
				for(var px:int=0; px < _content.xLen; px++){
					item.push(ba.readByte());
				}
			}
			_content.pathData = result;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		override protected function onIOError ( event : IOErrorEvent ) : void
		{
			dispatchEvent( new IOErrorEvent( IOErrorEvent.IO_ERROR ) );
		}
		
		override public function dispose () : void
		{
			loader.removeEventListener( Event.COMPLETE , onComplete );
			loader.removeEventListener( IOErrorEvent.IO_ERROR , onIOError );
			loader = null;
			super.dispose();
		}
	}
}